<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="充电桩" prop="stationId">
        <el-select
          v-model="queryParams.stationId"
          filterable
          placeholder="请选择充电桩"
          clearable
          :style="{ width: '100%' }"
        >
          <el-option
            v-for="(item, index) in stationOptions"
            :key="index"
            :label="item.label"
            :value="item.value"
            :disabled="item.disabled"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table
      v-loading="loading"
      :data="recordList"
      @selection-change="handleSelectionChange"
      size="small"
    >
      <el-table-column label="记录id" align="center" prop="recordId" />
      <el-table-column label="车牌号" align="center" prop="carCode" />
      <el-table-column label="充电区域" align="center" prop="areaName" />
      <el-table-column label="充电桩" align="center" prop="stationCode" />
      <el-table-column label="开始充电时间" align="center" prop="chargeStart" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.chargeStart) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束充电时间" align="center" prop="chargeEnd" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.chargeEnd) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="充电时长" align="center" :formatter="formatReportType">
      </el-table-column>
      <el-table-column label="充电花费" align="center" prop="money" />
      <el-table-column label="备注" align="center" prop="recordRemark" />
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listRecord } from "@/api/record";
import { listCar,carTotal } from "@/api/car";
import { stationTotal } from "@/api/station";
const dayjs = require("dayjs");
export default {
  name: "Record",
  data() {
    return {
      user: localStorage.getItem("user") ? JSON.parse(localStorage.getItem("user")) : {},
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 充电记录表格数据
      recordList: [],
      carOptions: [],
      stationOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        carId: null,
        stationId: null,
        chargeStart: null,
        chargeEnd: null,
        money: null,
        recordRemark: null,
      },
       // 查询参数
       carParams: {
        pageNum: 1,
        pageSize: 10,
        carCode: null,
        ownerName: null,
        ownerSex: null,
        ownerTel: null,
        carRemark: null,
      },
    };
  },
  created() {
    this.getList();
    this.getCarList();
    this.getStationList();
  },
  methods: {
    formatReportType(row) {
      let value = "";
      var lefttime = dayjs(row.chargeEnd).valueOf() - dayjs(row.chargeStart).valueOf();
      if (lefttime+"" == 'NaN') {
        return value;
       
      } else {
        var leftd = Math.floor(lefttime / (1000 * 60 * 60 * 24)); //计算天数
        var lefth = Math.floor((lefttime / (1000 * 60 * 60)) % 24); //计算小时数
        var leftm = Math.floor((lefttime / (1000 * 60)) % 60); //计算分钟数
        var lefts = Math.floor((lefttime / 1000) % 60); //计算秒数
        return leftd + "天" + lefth + "小时" + leftm + "分" + lefts + "秒";
      }
    },
    /** 查询电动车列表 */
    getCarList() {
      carTotal().then((response) => {
        const list = response.data.map((e) => {
          const obj = {
            value: e.carId,
            label: e.carCode,
          };
          return obj;
        });
        this.carOptions = list;
      });
    },
    /** 查询电动车列表 */
    getStationList() {
      stationTotal().then((response) => {
        const list = response.data.map((e) => {
          const obj = {
            value: e.stationId,
            label: e.stationCode,
          };
          return obj;
        });
        this.stationOptions = list;
      });
    },

    /** 查询充电记录列表 */
    async getList() {
      var username = this.user.username;
      console.log(username);
      this.carParams.ownerName = username;
      let result= await listCar(this.carParams);
      let carId = result.data.records[0].carId;
      this.loading = true;
      this.queryParams.carId = carId;
      listRecord(this.queryParams).then((response) => {
        this.recordList = response.data.records;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 表单重置
    reset() {
      this.form = {
        recordId: null,
        carId: null,
        stationId: null,
        chargeStart: null,
        chargeEnd: null,
        money: null,
        recordRemark: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
  },
};
</script>
